layui.use(['element','layer'], function() {
    var state=$("#state").val();
    var userId=$("#id").val();
    var userId1=$("#id1").val();
    var layer=layui.layer;
    function messageLoad() {
        if(state=="1"){
            //动态菜单
            layui.jquery.ajax({
                url: "/message/friendList",
                method: 'POST',
                data:{
                    id:userId
                },
                success: function(res) {
                    var html = "";
                    if (res!=""){
                        for(var i = 0; i < res.length; i++) {
                            var strli ="";
                            if (res[i].userId==userId){
                                strli+="<div class=\"left-group\"><img class=\"left-img\" src=\"/system/images/face.jpg\"/><div class=\"left-msg\"><span style=\"padding-left: 20px;\">"+res[i].userName+"</span><br/><i class=\"triangle-left\"></i><span class=\"left-reply\">"+res[i].message+"</span></div></div>";
                            }else{
                                strli+="<div class=\"right-group\"><div class=\"right-msg\"><span style=\"padding-right: 20px;\">"+res[i].userName+"</span><br/><i class=\"triangle-right\"></i><span class=\"right-reply\">"+res[i].message+"</span></div><img class=\"right-img\" src=\"/system/images/face.jpg\"/></div>";
                            }
                            html = html + strli;
                        }
                    }else {
                        html+="<p style=\"text-align: center;\">------------无历史消息------------</p><br />";
                    }
                    layui.jquery("#triangle").html(html);
                    layui.element.init(); //一定初始化一次
                }
            })
        }else {
            //动态菜单
            layui.jquery.ajax({
                url: "/message/groupList",
                method: 'POST',
                data:{
                    id:userId
                },
                success: function(res) {
                    var html = "";
                    if (res!=""){
                        for(var i = 0; i < res.length; i++) {
                            var strli ="";
                            if (res[i].userId==userId1){
                                strli+="<div class=\"right-group\"><div class=\"right-msg\"><span style=\"padding-right: 20px;\">"+res[i].userName+"</span><br/><i class=\"triangle-right\"></i><span class=\"right-reply\">"+res[i].message+"</span></div><img class=\"right-img\" src=\"/system/images/face.jpg\"/></div>";
                            }else{
                                strli+="<div class=\"left-group\"><img class=\"left-img\" src=\"/system/images/face.jpg\"/><div class=\"left-msg\"><span style=\"padding-left: 20px;\">"+res[i].userName+"</span><br/><i class=\"triangle-left\"></i><span class=\"left-reply\">"+res[i].message+"</span></div></div>";
                            }
                            html = html + strli;
                        }
                    }else {
                        html+="<p style=\"text-align: center;\">------------无历史消息------------</p><br />";
                    }
                    layui.jquery("#triangle").html(html);
                }
            })
        }
        $('#triangle').scrollTop( $('#triangle')[0].scrollHeight);
        $('#triangle').scrollLeft( $('#triangle')[0].scrollWidth);
    }
    window.setInterval(messageLoad, 100);
    function clickAdd(){
        var sendMessage=$("#sendMessage").val();
        if (sendMessage!=""){
            layer.msg('发送中，请稍候',{icon: 16,time:false,shade:0.8});
            layui.jquery.ajax({
                url: "/message/sendMessage",
                method: 'POST',
                data:{
                    userId:userId1,
                    receiveId:userId,
                    message:sendMessage,
                    state:state
                },
                success: function(res) {
                    layer.msg(res,{icon: 16,time:500,shade:0.8});
                    $("#sendMessage").val("");
                }
            })
        }else {
            layer.msg("不能发送空消息", {time: 1000});
        }
    }
    $("#subMessage").submit(function(){
        clickAdd();
    });

    $("#addFriend").click(function () {
        layer.open({
            title : "邀请好友",
            type : 2,
            area: ["700px", "520px"],
            content : "/group/toAddFriend?groupId="+userId,
            end: function () { //最后执行reload
                window.location.reload();
            }
        })
    });
    $("#exitGroup").click(function () {
        layer.confirm('确定退出群聊？',{icon:3, title:'提示信息'},function(index){
            $.post("/group/toExitGroup",{
                groupId : userId  //将需要删除的linkId作为参数传入
            },function(res){
                layer.msg(res,{icon: 16,time:false,shade:0.8});
                //延迟执行
                setTimeout(function(){
                    location.href = "/user/toTool";
                    layer.close(index);
                },1000);
            })
        })
    });
    $("#delFriend").click(function () {
        layer.confirm('确定删除好友？',{icon:3, title:'提示信息'},function(index){
            $.post("/user/toDelFriend",{
                friendId : userId  //将需要删除的linkId作为参数传入
            },function(res){
                layer.msg(res,{icon: 16,time:false,shade:0.8});
                //延迟执行
                setTimeout(function(){
                    location.href = "/user/toTool";
                    layer.close(index);
                },1000);
            })
        })
    });
});
